/**
 * @author 03010570
 * @date 2020/07/15
 * describe:    LeetCode : 883 、三维型体投影面积    https://leetcode-cn.com/problems/projection-area-of-3d-shapes/
 */
public class LeetCode_883 {
    public static void main(String[] args) {
        int[][] ints = new int[][]{{1, 2}, {3, 4}};
        System.out.println(projectionArea(ints));
        int[][] ints2 = new int[][]{{1,0}, {0,2}};
        System.out.println(projectionArea(ints2));
    }

    /**
     * 记录 X 轴的面积 xArea
     * 记录 Y 轴的面积 yArea
     * 记录 z 轴的面积 zArea
     * 总面积求和
     * 时间复杂度 ： O(N^2)    N 为 grid 数组长度
     * 空间度咋读 ： O(1)
     *
     * @param grid
     * @return
     */
    public static int projectionArea(int[][] grid) {
        int xArea = 0;
        int yArea = 0;
        int zArea = 0;
        for (int i = 0; i < grid.length; i++) {
            int maxRow = 0;
            int maxColumn = 0;
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] != 0) {
                    xArea++;
                }
                if (grid[j][i] > maxRow) {
                    maxRow = grid[j][i];
                }
                if (grid[i][j] > maxColumn) {
                    maxColumn = grid[i][j];
                }
            }
            yArea += maxRow;
            zArea += maxColumn;
        }
        return xArea + yArea + zArea;
    }
}
